#include "elementstruct.h"
#include <QPushButton>
#include <QString>


ElementStruct::ElementStruct(QWidget *parent, PBO* pbo)
	: QWidget(parent)
{
	ui.setupUi(this);

	checkbox = new QCheckBox*[9];
	for(int i =0; i< 9;i++)
	{
		checkbox[i] = new QCheckBox(this);
		checkbox[i]->move((i%3) * 20 , (i/3) * 20);
		QObject::connect(checkbox[i],SIGNAL(clicked()), this,SLOT(changeElem()));
	}

	opType = new QRadioButton*[4];
	
	QString str[] = {QString("Dylacja"), QString("Erozja"),  QString("Otwarcie"), QString("Zamkniecie")};
	for(int i =0; i< 4;i++)
	{
		opType[i] = new QRadioButton(str[i], this);
		opType[i]->move(i * 70 , 70);
		QObject::connect(opType[i],SIGNAL(clicked()), this,SLOT(changeElem()));
	}
	opType[0]->setChecked(true);
	


	QPushButton* buttonOK = new QPushButton("OK", this);
	buttonOK->move(20,90);

	QObject::connect(buttonOK,SIGNAL(clicked()), this,SLOT(OK_clicked()));

	QPushButton* cancelOK = new QPushButton("Cancel", this);
	cancelOK->move(100,90);

	QObject::connect(cancelOK,SIGNAL(clicked()), this,SLOT(close()));

	this->pbo = pbo;
}

void ElementStruct::changeElem()
{
	run(false);
}

void ElementStruct::run(bool pernam)
{
	bool tab[9];
	for(int i=0; i<9;i++) tab[i] = checkbox[i]->isChecked();
	DilationErosionEnum type;
	if(opType[0]->isChecked()) type = DILATION;
	if(opType[1]->isChecked()) type = EROSION;
	if(opType[2]->isChecked()) type = OPENING;
	if(opType[3]->isChecked()) type = CLOSING;
	pbo->dilation_erosion(tab,3,pernam,type);
}

void ElementStruct::close()
{
	pbo->identity();
	QWidget::close();
	this->deleteLater();
}

void ElementStruct::OK_clicked()
{
	run(true);
	QWidget::close();
	this->deleteLater();
}

ElementStruct::~ElementStruct()
{

}
